using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;

using System.Reflection;
using Gizmox.WebGUI.Common.Resources;
using Gizmox.WebGUI.Forms;

using System.IO;
using TVC.Layout;

namespace TVC.Modules.Com
{
    /// <summary>
    /// Summary description for CustomWindowPanelControl.
    /// </summary>

    [Serializable()]
    public class MLC : ModuleParent, IHostedApplication
    {
        private TabControl tabControl1;
        private TabPage tbList;
        private TabPage tbDetail;
        private Label label3;
        private TextBox txtLOOKUP;
        private Label label2;
        private TextBox txtDESCRIPTION;
        private TextBox txtCODE;
        private Label label1;
        private Label label10;
        private TextBox txtCATEGORY;
        private Label label8;
        private Label label7;
        private TextBox txtHEADER;
        private DataGridView dgvList;
        private PictureBox pibCATEGORY;
        private DataGridViewTextBoxColumn colCode;
        private DataGridViewTextBoxColumn colLookup;
        private DataGridViewTextBoxColumn colDescription;
        private BindingSource bisCAT;
        private IContainer components;
        private CheckBox ckbNONVALIDATED;
        private PictureBox pibCode;
        GridLayout layout = null;
        public MLC()
        {
            // This call is required by the WebGUI Form Designer.
            InitializeComponent();
            _strType = "MLC";
        }

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }
        int 	idAddNew=484;
        int 	idEdit=485;
        int 	idDelete=488;
        int 	idclsTransferIn=489;
        int 	idclsTransferOut=490;
        int 	idPrint=491;
        int idAllowEditLanguage = 1;
        

        protected override void AllowEditLanguage()
        {
            foreach (Control x in tbDetail.Controls)
            {
                if (x is Label)
                {
                    x.DoubleClick += Control_DoubleClick;
                }
                if (x is GroupBox || x is ExpandableGroupBox || x is Panel)
                {
                    x.DoubleClick += Control_DoubleClick;
                    foreach (Control y in x.Controls)
                    {
                        if (y is Label)
                            y.DoubleClick += Control_DoubleClick;
                    }
                }
            }
            
            ContextMenu menu = new ContextMenu();
            menu.MenuItems.Add(new MenuItem("Edit Language"));
            dgvList.MenuClick += dgvList_MenuClick;
            tabControl1.DoubleClick += tabMain_DoubleClick;
            dgvList.ContextMenu = menu;
        }

        protected override void LoadLanguage(string language)
        {
            foreach (Control x in tbDetail.Controls)
            {
                if (x is Label)
                {
                    clsConfigurarion.GetLabel(this.ToString(), x, language);
                }
                if (x is GroupBox || x is ExpandableGroupBox || x is Panel)
                {
                    clsConfigurarion.GetLabel(this.ToString(), x, language);
                    foreach (Control y in x.Controls)
                    {
                        if (y is Label)
                            clsConfigurarion.GetLabel(this.ToString(), y, language);
                    }
                }
            }            

            if (this.Parent is HeaderedPanel)
            {
                HeaderedPanel pln = this.Parent as HeaderedPanel;
                _title = clsConfigurarion.GetLabel(this.ToString(), "", null, language);
            }
        }
        public HostedToolBarElement[] GetToolBarElements()
        {
            ArrayList objElements = new ArrayList();
            if (IsPermis(idAddNew))
                objElements.Add(new HostedToolBarButton("Add New", new IconResourceHandle("Add.png"), "C"));
            if (IsPermis(idEdit) )
                objElements.Add(new HostedToolBarButton("Edit", new IconResourceHandle("Edit.png"), "A"));
            if (IsPermis(idAddNew) || IsPermis(idEdit))
                objElements.Add(new HostedToolBarButton("Save", new IconResourceHandle("Save.png"), "S"));
            if (IsPermis(idAddNew))
                objElements.Add(new HostedToolBarButton("Copy", new IconResourceHandle("Copy.png"), "O"));
            if (IsPermis(idDelete))
                objElements.Add(new HostedToolBarButton("Delete", new IconResourceHandle("Delete.png"), "X"));
            objElements.Add(new HostedToolBarSeperator());
            if (IsPermis(idclsTransferIn))
                objElements.Add(new HostedToolBarButton("clsTransfer In", new IconResourceHandle("Down.png"), "TI"));
            if (IsPermis(idclsTransferOut))
                objElements.Add(new HostedToolBarButton("clsTransfer Out", new IconResourceHandle("Up.png"), "TO"));
            objElements.Add(new HostedToolBarSeperator());
            if (IsPermis(idPrint))
                objElements.Add(new HostedToolBarButton("Print", new IconResourceHandle("Print.png"), "P"));
            objElements.Add(new HostedToolBarButton("Refresh", new IconResourceHandle("Refresh.png"), "R"));

            return (HostedToolBarElement[])objElements.ToArray(typeof(HostedToolBarElement));
        }
		
		

        #region Component Designer generated code
        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MLC));
            this.tabControl1 = new Gizmox.WebGUI.Forms.TabControl();
            this.tbList = new Gizmox.WebGUI.Forms.TabPage();
            this.dgvList = new Gizmox.WebGUI.Forms.DataGridView();
            this.colCode = new Gizmox.WebGUI.Forms.DataGridViewTextBoxColumn();
            this.colLookup = new Gizmox.WebGUI.Forms.DataGridViewTextBoxColumn();
            this.colDescription = new Gizmox.WebGUI.Forms.DataGridViewTextBoxColumn();
            this.bisCAT = new Gizmox.WebGUI.Forms.BindingSource(this.components);
            this.tbDetail = new Gizmox.WebGUI.Forms.TabPage();
            this.pibCode = new Gizmox.WebGUI.Forms.PictureBox();
            this.ckbNONVALIDATED = new Gizmox.WebGUI.Forms.CheckBox();
            this.label3 = new Gizmox.WebGUI.Forms.Label();
            this.label8 = new Gizmox.WebGUI.Forms.Label();
            this.label10 = new Gizmox.WebGUI.Forms.Label();
            this.txtLOOKUP = new Gizmox.WebGUI.Forms.TextBox();
            this.label7 = new Gizmox.WebGUI.Forms.Label();
            this.txtCATEGORY = new Gizmox.WebGUI.Forms.TextBox();
            this.pibCATEGORY = new Gizmox.WebGUI.Forms.PictureBox();
            this.label2 = new Gizmox.WebGUI.Forms.Label();
            this.txtDESCRIPTION = new Gizmox.WebGUI.Forms.TextBox();
            this.txtCODE = new Gizmox.WebGUI.Forms.TextBox();
            this.txtHEADER = new Gizmox.WebGUI.Forms.TextBox();
            this.label1 = new Gizmox.WebGUI.Forms.Label();
            ((System.ComponentModel.ISupportInitialize)(this.tabControl1)).BeginInit();
            this.tabControl1.SuspendLayout();
            this.tbList.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.dgvList)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.bisCAT)).BeginInit();
            this.tbDetail.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.pibCode)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.pibCATEGORY)).BeginInit();
            this.SuspendLayout();
            // 
            // tabControl1
            // 
            this.tabControl1.Alignment = Gizmox.WebGUI.Forms.TabAlignment.Bottom;
            this.tabControl1.Appearance = Gizmox.WebGUI.Forms.TabAppearance.Workspace;
            this.tabControl1.Controls.Add(this.tbList);
            this.tabControl1.Controls.Add(this.tbDetail);
            this.tabControl1.Dock = Gizmox.WebGUI.Forms.DockStyle.Fill;
            this.tabControl1.Location = new System.Drawing.Point(3, 3);
            this.tabControl1.Name = "tabControl1";
            this.tabControl1.SelectedIndex = 0;
            this.tabControl1.Size = new System.Drawing.Size(601, 442);
            this.tabControl1.TabIndex = 0;
            // 
            // tbList
            // 
            this.tbList.Controls.Add(this.dgvList);
            this.tbList.Dock = Gizmox.WebGUI.Forms.DockStyle.Fill;
            this.tbList.Location = new System.Drawing.Point(4, 22);
            this.tbList.Name = "tbList";
            this.tbList.Size = new System.Drawing.Size(593, 416);
            this.tbList.TabIndex = 0;
            this.tbList.Text = "List";
            // 
            // dgvList
            // 
            this.dgvList.AllowUserToAddRows = false;
            this.dgvList.AutoGenerateColumns = false;
            this.dgvList.BackgroundColor = System.Drawing.Color.White;
            this.dgvList.BorderStyle = Gizmox.WebGUI.Forms.BorderStyle.FixedSingle;
            this.dgvList.ColumnHeadersHeightSizeMode = Gizmox.WebGUI.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dgvList.Columns.AddRange(new Gizmox.WebGUI.Forms.DataGridViewColumn[] {
            this.colCode,
            this.colLookup,
            this.colDescription});
            this.dgvList.DataSource = this.bisCAT;
            this.dgvList.Dock = Gizmox.WebGUI.Forms.DockStyle.Fill;
            this.dgvList.Location = new System.Drawing.Point(0, 0);
            this.dgvList.Name = "dgvList";
            //this.//dgvList.ReadOnly = true;
            this.dgvList.RowHeadersWidth = 25;
            this.dgvList.RowTemplate.DefaultCellStyle.FormatProvider = new System.Globalization.CultureInfo("en-US");
            this.dgvList.ShowFilterRow = true;
            this.dgvList.Size = new System.Drawing.Size(593, 416);
            this.dgvList.TabIndex = 0;
            this.dgvList.RowHeaderMouseDoubleClick += new Gizmox.WebGUI.Forms.DataGridViewCellMouseEventHandler(this.dgvList_RowHeaderMouseDoubleClick);
            // 
            // colCode
            // 
            this.colCode.DataPropertyName = "Code";
            this.colCode.HeaderText = "Code";
            this.colCode.Name = "colCode";
            this.colCode.ReadOnly = true;
            // 
            // colLookup
            // 
            this.colLookup.DataPropertyName = "Lookup";
            this.colLookup.HeaderText = "Look Up";
            this.colLookup.Name = "colLookup";
            this.colLookup.ReadOnly = true;
            // 
            // colDescription
            // 
            this.colDescription.AutoSizeMode = Gizmox.WebGUI.Forms.DataGridViewAutoSizeColumnMode.Fill;
            this.colDescription.DataPropertyName = "Description";
            this.colDescription.HeaderText = "Description";
            this.colDescription.Name = "colDescription";
            this.colDescription.ReadOnly = true;
            // 
            // bisCAT
            // 
            this.bisCAT.DataSource = typeof(DTO.MLCInfo);
            // 
            // tbDetail
            // 
            this.tbDetail.Controls.Add(this.pibCode);
            this.tbDetail.Controls.Add(this.ckbNONVALIDATED);
            this.tbDetail.Controls.Add(this.label3);
            this.tbDetail.Controls.Add(this.label8);
            this.tbDetail.Controls.Add(this.label10);
            this.tbDetail.Controls.Add(this.txtLOOKUP);
            this.tbDetail.Controls.Add(this.label7);
            this.tbDetail.Controls.Add(this.txtCATEGORY);
            this.tbDetail.Controls.Add(this.pibCATEGORY);
            this.tbDetail.Controls.Add(this.label2);
            this.tbDetail.Controls.Add(this.txtDESCRIPTION);
            this.tbDetail.Controls.Add(this.txtCODE);
            this.tbDetail.Controls.Add(this.txtHEADER);
            this.tbDetail.Controls.Add(this.label1);
            this.tbDetail.Dock = Gizmox.WebGUI.Forms.DockStyle.Fill;
            this.tbDetail.Location = new System.Drawing.Point(0, 0);
            this.tbDetail.Name = "tbDetail";
            this.tbDetail.Size = new System.Drawing.Size(593, 416);
            this.tbDetail.TabIndex = 1;
            this.tbDetail.Text = "Detail";
            // 
            // pibCode
            // 
            this.pibCode.BackColor = System.Drawing.Color.Transparent;
            this.pibCode.BackgroundImageLayout = Gizmox.WebGUI.Forms.ImageLayout.Center;
            this.pibCode.Image = new Gizmox.WebGUI.Common.Resources.ImageResourceHandle(resources.GetString("pibCode.Image"));
            this.pibCode.Location = new System.Drawing.Point(303, 12);
            this.pibCode.Name = "pibCode";
            this.pibCode.Size = new System.Drawing.Size(20, 20);
            this.pibCode.SizeMode = Gizmox.WebGUI.Forms.PictureBoxSizeMode.CenterImage;
            this.pibCode.TabIndex = 2;
            this.pibCode.TabStop = false;
            this.pibCode.Click += new System.EventHandler(this.pibCode_Click);
            // 
            // ckbNONVALIDATED
            // 
            this.ckbNONVALIDATED.AutoSize = true;
            this.ckbNONVALIDATED.CheckState = Gizmox.WebGUI.Forms.CheckState.Unchecked;
            this.ckbNONVALIDATED.Location = new System.Drawing.Point(89, 169);
            this.ckbNONVALIDATED.Name = "ckbNONVALIDATED";
            this.ckbNONVALIDATED.Size = new System.Drawing.Size(32, 17);
            this.ckbNONVALIDATED.TabIndex = 5;
            this.ckbNONVALIDATED.Text = "_";
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(16, 41);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(35, 13);
            this.label3.TabIndex = 0;
            this.label3.Text = "Look Up";
            // 
            // label8
            // 
            this.label8.AutoSize = true;
            this.label8.Location = new System.Drawing.Point(16, 142);
            this.label8.Name = "label5";
            this.label8.Size = new System.Drawing.Size(46, 13);
            this.label8.TabIndex = 0;
            this.label8.Text = "Header";
            // 
            // label10
            // 
            this.label10.AutoSize = true;
            this.label10.Location = new System.Drawing.Point(16, 77);
            this.label10.Name = "label10";
            this.label10.Size = new System.Drawing.Size(32, 13);
            this.label10.TabIndex = 0;
            this.label10.Text = "Category";
            // 
            // txtLOOKUP
            // 
            this.txtLOOKUP.Location = new System.Drawing.Point(89, 38);
            this.txtLOOKUP.MaxLength = 15;
            this.txtLOOKUP.Name = "txtLOOKUP";
            this.txtLOOKUP.Size = new System.Drawing.Size(100, 20);
            this.txtLOOKUP.TabIndex = 1;
            // 
            // label7
            // 
            this.label7.AutoSize = true;
            this.label7.Location = new System.Drawing.Point(16, 170);
            this.label7.Name = "label6";
            this.label7.Size = new System.Drawing.Size(46, 13);
            this.label7.TabIndex = 0;
            this.label7.Text = "Non Validated";
            // 
            // txtCATEGORY
            // 
            this.txtCATEGORY.Location = new System.Drawing.Point(89, 74);
            this.txtCATEGORY.MaxLength = 15;
            this.txtCATEGORY.Name = "txtCATEGORY";
            this.txtCATEGORY.Size = new System.Drawing.Size(149, 20);
            this.txtCATEGORY.TabIndex = 2;
            // 
            // pibCATEGORY
            // 
            this.pibCATEGORY.BackColor = System.Drawing.Color.Transparent;
            this.pibCATEGORY.BackgroundImageLayout = Gizmox.WebGUI.Forms.ImageLayout.Center;
            this.pibCATEGORY.Image = new Gizmox.WebGUI.Common.Resources.ImageResourceHandle(resources.GetString("pibCATEGORY.Image"));
            this.pibCATEGORY.Location = new System.Drawing.Point(241, 74);
            this.pibCATEGORY.Name = "pibCATEGORY";
            this.pibCATEGORY.Size = new System.Drawing.Size(20, 20);
            this.pibCATEGORY.SizeMode = Gizmox.WebGUI.Forms.PictureBoxSizeMode.CenterImage;
            this.pibCATEGORY.TabIndex = 2;
            this.pibCATEGORY.TabStop = false;
            this.pibCATEGORY.Click += new System.EventHandler(this.pibCATEGORY_Click);
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(16, 103);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(35, 13);
            this.label2.TabIndex = 0;
            this.label2.Text = "Description";
            // 
            // txtDESCRIPTION
            // 
            this.txtDESCRIPTION.Location = new System.Drawing.Point(89, 100);
            this.txtDESCRIPTION.Name = "txtDESCRIPTION";
            this.txtDESCRIPTION.Size = new System.Drawing.Size(436, 20);
            this.txtDESCRIPTION.TabIndex = 3;
            // 
            // txtCODE
            // 
            this.txtCODE.Location = new System.Drawing.Point(89, 12);
            this.txtCODE.MaxLength = 50;
            this.txtCODE.Name = "txtCODE";
            this.txtCODE.Size = new System.Drawing.Size(211, 20);
            this.txtCODE.TabIndex = 0;
            // 
            // txtHEADER
            // 
            this.txtHEADER.Location = new System.Drawing.Point(89, 139);
            this.txtHEADER.MaxLength = 10;
            this.txtHEADER.Name = "txtHEADER";
            this.txtHEADER.Size = new System.Drawing.Size(123, 20);
            this.txtHEADER.TabIndex = 4;
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(16, 15);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(35, 13);
            this.label1.TabIndex = 0;
            this.label1.Text = "Code";
            // 
            // MLC
            // 
            this.Controls.Add(this.tabControl1);
            this.DockPadding.All = 3;
            this.Location = new System.Drawing.Point(0, -10);
            this.Padding = new Gizmox.WebGUI.Forms.Padding(3);
            this.Size = new System.Drawing.Size(607, 448);
            this.Load += new System.EventHandler(this.CAT_Load);
            ((System.ComponentModel.ISupportInitialize)(this.tabControl1)).EndInit();
            this.tabControl1.ResumeLayout(false);
            this.tbList.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.dgvList)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.bisCAT)).EndInit();
            this.tbDetail.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.pibCode)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.pibCATEGORY)).EndInit();
            this.ResumeLayout(false);

        }
        #endregion


        #region IHostedApplication Members

        public void InitializeApplication()
        {
        }
        //public string GetTitle()
        //{
        //    return "Mapping Category";
        //}
        public void addRecord()
        {

        }
        public void SetObject(object obj)
        {
        }
        

        public void OnToolBarButtonClick(HostedToolBarButton objButton, EventArgs objEvent)
        {
            try
            {
                sErr = "";
                string strAction = (string)objButton.Tag;

                BUS.MLCControl ctr = new BUS.MLCControl();
                switch (strAction)
                {
                    case "C":
                        tabControl1.SelectedTab = tbDetail;
                        RefeshForm("");
                        EnableForm(true);
                        break;
                    case "A":
                        if (dgvList.CurrentCell != null)
                        {
                            DTO.MLCInfo info = new DTO.MLCInfo((DataRowView)dgvList.CurrentRow.DataBoundItem);
                            if (txtCODE.Text != info.CODE)
                                SetData(info);

                            EnableForm(true);
                            txtCODE.ReadOnly = true;
                            tabControl1.SelectedTab = tbDetail;
                        }
                        break;
                    case "S":
                          if (layout == null)
                            layout = new GridLayout(MainForm.ServerPath + "Resources\\Layout\\", MainForm.ServerPath + "Documents\\Layout\\" + GetMainForm().PODInfo.USER_ID + "\\", _strType);
                        layout.GetUpDGV(dgvList);
                        layout.SaveLayout();
                        Save(ctr);

                        break;
                    case "O":
                        if (dgvList.CurrentCell != null)
                        {
                            DTO.MLCInfo info = new DTO.MLCInfo((DataRowView)dgvList.CurrentRow.DataBoundItem);
                            SetData(info);
                            EnableForm(true);
                            tabControl1.SelectedTab = tbDetail;
                        }
                        break;
                    case "X":
                        Delete(ctr);

                        break;
                    case "TI":
                        Forms.frmTransferIn frmTI = new Forms.frmTransferIn(_strType,GetMainForm());
                        frmTI.ShowDialog();
                        break;
                    case "TO":
                        Forms.frmTransferOut frmTO = new Forms.frmTransferOut(_strType, GetMainForm());
                        frmTO.Show();
                        break;
                    case "R":
                        RefreshList();
                        break;
                    case "P":
                        if (dgvList.CurrentCell != null)
                        {
                            DTO.MLCInfo info = new DTO.MLCInfo((DataRowView)dgvList.CurrentRow.DataBoundItem);
                            DataTable dt = DTO.MLCInfo.ToDataTable();
                            dt.Rows.Add(info.ToDataRow(dt));
                            DataSet ds = new DataSet();
                            ds.Tables.Add(dt);
                            MainForm main = GetMainForm();
                            if (main != null)
                                main.PrintDataSet(ds, _strType);
                        }
                        break;
                }
                _processStatus = strAction;
                if (sErr == "")
                    ThrowMsg("Command: " + _processStatus);
            }
            catch (Exception ex)
            {
                ThrowMsg(ex.Message + Environment.NewLine + ex.Source);
            }
        }

        private void RefreshList()
        {
            GetMainForm().LoadDataTable(_strType);
            Rebind();
            if (layout == null)
                layout = new GridLayout(MainForm.ServerPath + "Resources\\Layout\\", MainForm.ServerPath + "Documents\\Layout\\" + GetMainForm().PODInfo.USER_ID + "\\", _strType);
            layout.ResetLayout();
            layout.SetUpDGV(dgvList);
        }

        private void Delete(BUS.MLCControl ctr)
        {
            if (dgvList.CurrentCell != null)
            {
                DTO.MLCInfo info = new DTO.MLCInfo((DataRowView)dgvList.CurrentRow.DataBoundItem);
                if (ctr.IsExist(GetMainForm().DB, info.CODE))
                {
                    Forms.frmMsg frm = new Forms.frmMsg();
                    frm.FormClosed += delegate(object form, FormClosedEventArgs args)
                    {
                        if (((Gizmox.WebGUI.Forms.Form)form).DialogResult == DialogResult.OK)
                        {
                            ctr.Delete(GetMainForm().DB, info.CODE);
                            if (sErr == "")
                            {
                                GetMainForm().RemoveRecord(_strType,((DataRowView)dgvList.CurrentRow.DataBoundItem).Row);
                                EnableForm(false);
                                Rebind();
                                sErr = "Delete record " + info.CODE + " successful";
                            }

                            ThrowMsg(sErr);

                        }

                    };
                    frm.ShowMsg("Do you want to delete " + info.CODE + "?");

                }
                else
                    sErr =clsConfigurarion.GetErrMsg("E0002", language);// "Data is not exist";
            }
        }

        private void Save(BUS.MLCControl ctr)
        {
            if (tabControl1.SelectedTab == tbDetail)
            {
                DTO.MLCInfo info = GetData();
                if (_processStatus == "A")
                {
                    if (ctr.IsExist(GetMainForm().DB, txtCODE.Text))
                    {
                        sErr = ctr.Update(info);
                        UpdateRecord(info);
                        sErr = "Update record " + txtCODE.Text + " successful";
                    }
                    else
                        sErr =clsConfigurarion.GetErrMsg("E0002", language);// "Data is not exist";
                }
                else if (_processStatus == "C" || _processStatus == "O")
                {
                    if (!ctr.IsExist(GetMainForm().DB, txtCODE.Text))
                    {
                        ctr.Add(info, ref sErr);
                        UpdateRecord(info);
                        sErr = "Add record " + txtCODE.Text + " successful";
                    }
                    else
                        sErr = clsConfigurarion.GetErrMsg("E0007", language);
                }

                ThrowMsg(sErr);

            }
        }

        private void UpdateRecord(DTO.MLCInfo info)
        {
             GetMainForm().GetTable(_strType).BeginLoadData();
             GetMainForm().UpdateRecord(_strType,info.ToDataRow( GetMainForm().GetTable(_strType)).ItemArray, LoadOption.OverwriteChanges);
             GetMainForm().GetTable(_strType).EndLoadData();
            EnableForm(false);
            Rebind();
        }

        private void Rebind()
        {
            bisCAT.DataSource =  GetMainForm().GetTable(_strType);
        }

        private void ThrowMsg(string sErr)
        {
            BaseForm x = this.Parent.Parent as BaseForm;
            if (x != null)
            {
                x.SetMsg(sErr);
            }
        }

        private DTO.MLCInfo GetData()
        {
            return new DTO.MLCInfo(GetMainForm().DB
                , txtLOOKUP.Text
                , GetMainForm().PODInfo.USER_ID
                , GetMainForm().PODInfo.USER_ID
                , BUS.CommonControl.ConvertDateToInt(DateTime.Now)
                , BUS.CommonControl.ConvertDateToInt(DateTime.Now)
                , txtCODE.Text
                , txtDESCRIPTION.Text
                , txtHEADER.Text
                , ckbNONVALIDATED.Checked ? "Y" : "N"
                , txtCATEGORY.Text
                );

        }

        private void EnableForm(bool p)
        {
            txtCODE.ReadOnly = !p;
            txtLOOKUP.ReadOnly = !p;
            txtDESCRIPTION.ReadOnly = !p;
            txtHEADER.ReadOnly = !p;
            ckbNONVALIDATED.Enabled = p;
            txtCATEGORY.ReadOnly = !p;

            pibCATEGORY.Enabled = p;
            pibCode.Enabled = p;
        }

        private void SetData(DTO.MLCInfo info)
        {
            txtCODE.Text = info.CODE;
            txtLOOKUP.Text = info.LOOKUP;
            txtDESCRIPTION.Text = info.DESCRIPTION;
            txtHEADER.Text = info.HEADER;
            ckbNONVALIDATED.Checked = info.NONVALIDATED == "Y";
            txtCATEGORY.Text = info.CATEGORY;
        }

        private void RefeshForm(string str)
        {
            txtCODE.Text = str;
            txtLOOKUP.Text = str;
            txtDESCRIPTION.Text = str;
            txtHEADER.Text = str;
            ckbNONVALIDATED.Checked = true;
            txtCATEGORY.Text = str;
        }

        #endregion

        private void CAT_Load(object sender, EventArgs e)
        {
            dgvList.AutoGenerateColumns = false;
            if (!GetMainForm().IsContainsTables(_strType))
                GetMainForm().LoadDataTable(_strType);
            Rebind();
            layout = new GridLayout(MainForm.ServerPath + "Resources\\Layout\\", MainForm.ServerPath + "Documents\\Layout\\" + GetMainForm().PODInfo.USER_ID + "\\", _strType);
            //layout.LoadLayout();
            layout.SetUpDGV(dgvList);
            EnableForm(false);

            if (idAllowEditLanguage >= GetMainForm().Permission.Length || GetMainForm().Permission[idAllowEditLanguage] == 'Y')
                AllowEditLanguage();
        
        }



        private void dgvList_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (dgvList.CurrentCell != null)
            {
                DTO.MLCInfo info = new DTO.MLCInfo((DataRowView)dgvList.CurrentRow.DataBoundItem);
                SetData(info);
                EnableForm(false);
                tabControl1.SelectedTab = tbDetail;
                _processStatus = "V";
            }
        }

        private void pibCATEGORY_Click(object sender, EventArgs e)
        {
            Forms.frmView frm = new Forms.frmView("CAT",GetMainForm());
            frm.FormClosed += delegate(object form, FormClosedEventArgs args)
            {
                if (((Forms.frmView)form).DialogResult == DialogResult.OK)
                {
                    if (txtCATEGORY.Text == "")
                        ckbNONVALIDATED.Checked = false;
                    txtCATEGORY.Text = ((Forms.frmView)form).ReturnValue[((Forms.frmView)form).ReturnValue.Table.PrimaryKey[0]].ToString();
                    txtDESCRIPTION.Text = ((Forms.frmView)form).ReturnValue[DTO.CategoryInfo.Field.Description.ToString()].ToString();
                    txtHEADER.Text = ((Forms.frmView)form).ReturnValue[DTO.CategoryInfo.Field.Header.ToString()].ToString();
                }
            };
            frm.ShowDialog();
        }

        private void pibCode_Click(object sender, EventArgs e)
        {
            Forms.frmSYS frm = new Forms.frmSYS();
            frm.FormClosed += delegate(object form, FormClosedEventArgs args)
            {
                if (((Forms.frmSYS)form).DialogResult == DialogResult.OK)
                {
                    txtCODE.Text = ((Forms.frmSYS)form).ReturnValue[((Forms.frmSYS)form).ReturnValue.Table.PrimaryKey[0]].ToString();
                }
            };
            frm.ShowDialog();
        }
        public string GetLanguage()
        {
            return language;
        }
    }
}
